package wsepForum.ui.jaxcent.states;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.Vector;

import jaxcent.HtmlDiv;
import jaxcent.HtmlElement;
import jaxcent.HtmlInputButton;
import jaxcent.HtmlSelect;
import jaxcent.JaxcentPage;
import jaxcent.Jaxception;
import wsepForum.ui.jaxcent.IXContext;

/**
 * @author Oren
 *
 */
public abstract class AXQuery implements IXState {

	/*****************************
	 * MEMBERS                   *
	 *****************************/
	// made it static to save loading time
	protected static final String MONTHS =
		"<option>1</option><option>2</option><option>3</option><option>4</option>"+
		"<option>5</option><option>6</option><option>7</option><option>8</option>"+
		"<option>9</option><option>10</option><option>11</option><option>12</option>";
	protected static final String DAYS = MONTHS+"<option>13</option><option>14</option><option>15</option><option>16</option>"+
	"<option>17</option><option>18</option><option>19</option><option>20</option>"+
	"<option>21</option><option>22</option><option>23</option><option>24</option>"+
	"<option>25</option><option>26</option><option>27</option><option>28</option>"+
	"<option>29</option><option>30</option><option>31</option>";
	protected static final String YEARS =
	"<option>2009</option><option>2010</option><option>2011</option><option>2012</option>";

	private HtmlElement xUserField;
	private HtmlSelect xUserSelect;
	protected HtmlElement xDesc;
	protected HtmlDiv xResults;
	protected int type;
	private boolean usersLoaded;
	protected IXContext context;

	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public AXQuery(IXContext context) {
		this.context = context;
		this.usersLoaded = false;
		initContent();
	}

	/*****************************
	 * METHODS                   *
	 *****************************/
	/* (non-Javadoc)
	 * @see wsepForum.network.server.jax.pages.states.XState#getType()
	 */
	@Override
	public StateType getType() {
		return StateType.QUERY;
	}

	protected void initContent() {
		construct();
		final JaxcentPage page = this.context.getPage();
		this.xDesc = new HtmlElement(page,"xdescrp");
		this.xUserField = new HtmlElement(page,"xuserfield");
		this.xUserSelect = new HtmlSelect(page,"xuser");
		this.xResults = new HtmlDiv(page,"xresults");
		try {
			new HtmlSelect(page,"xtype") {
				@Override
				protected void onChange() {
					try {
						int index = getSelectedIndex();
						dealWithTypeChange(index);
					} catch (Jaxception e) {
						page.showMessageDialog("cannot get selected line!\n"+e.getMessage());
						context.loadSection(0);
					}
				}
			}.focus();
		} catch (Jaxception e) {/*ignore focusing exception */}
			new HtmlInputButton(page, "xquery" ) {
			    @SuppressWarnings("unchecked")
				protected void onClick(Map pageData) {
			    	try {
						setEnabled(false);
						setValue("Quering...");
						dealWithQuery(pageData);
						setValue("Query");
						setEnabled(true);
					} catch (Jaxception e) {
						page.showMessageDialog("cannot deal with query button!\n"+e.getMessage());
						context.loadSection(0);
					}
			    }
			};
	}

	@SuppressWarnings("unchecked")
	protected abstract void dealWithQuery(Map pageData);

	protected abstract void dealWithTypeChange(int index);

	protected abstract void construct();


	protected void construct(String title, String extraFields,String types) {
		this.context.setBody(
			"<form id=\"query\"><div class=\"panel\">"+
			"<div class=\"inner\"><span class=\"corners-top\"><span></span></span>"+
			"<div class=\"content\">"+
			"<h2>"+title+"</h2><fieldset class=\"fields1\"><dl>"+
			"<dt><label for=\"xtype\">Type:</label></dt>"+
			"<dd><select tabindex=\"1\" id=\"xtype\">"+types+"</select></dd>"+
			"</dl><dl><dt><label for=\"xdescrp\">Description:</label></dt>"+
			"<dd><strong id=\"xdescrp\">Loading...</strong></dd>"+
			"</dl></fieldset></div>"+
			"<span class=\"corners-bottom\"><span></span></span></div></div>"+
			"<div class=\"panel\"><div class=\"inner\"><span class=\"corners-top\"><span></span></span>"+
			"<div class=\"content\"><fieldset class=\"fields2\">" +
			"<dl id=\"xdate\"><dt><label for=\"xdate\">Date:</label></dt>"+
			"<dd>Day <select tabindex=\"2\" id=\"xdate1day\">"+DAYS+"</select>"+
			"&nbsp;Month <select tabindex=\"3\" id=\"xdate1month\">"+MONTHS+"</select>"+
			"&nbsp;Year <select tabindex=\"4\" id=\"xdate1year\">"+YEARS+"</select></dd></dl>"+
			extraFields + "<dl id=\"xuserfield\" style=\"display: none;\"><dt><label for=\"xuser\">User:</label></dt>"+
			"<dd><select tabindex=\"8\" id=\"xuser\"></select></dd></dl>"+
			"</fieldset></div>"+
			"<span class=\"corners-bottom\"><span></span></span></div>"+
			"</div><fieldset class=\"submit-buttons\">"+
			"<input tabindex=\"10\" type=\"button\" id=\"xquery\" value=\"Query\" class=\"button1\" /></fieldset>"+
			"<div id=\"xresults\"></div></form>");
	}

	protected void loadUsers(boolean show) {
		try {
			if (show) {
				if (!this.usersLoaded) {
					Vector<String> users = this.context.getSession().getHandler().getAllUsernames();
					String user;
					for (int i=0;i<users.size();i++) {
						user = users.elementAt(i);
						this.xUserSelect.insertOption(i, user);
					}
					this.usersLoaded = true;
				}
				this.xUserField.show();
			} else {
				this.xUserField.hide();
			}
		} catch (Jaxception e) {
			this.context.getPage().showMessageDialog("Problem with showing users list\n"+e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	protected Date getDate(int fieldNum, Map pageData) {
		int year = Integer.valueOf(((String)pageData.get("xdate"+fieldNum+"year")).split(":")[1]);
		int month = Integer.valueOf(((String)pageData.get("xdate"+fieldNum+"month")).split(":")[1]);
		int day = Integer.valueOf(((String)pageData.get("xdate"+fieldNum+"day")).split(":")[1]);
		GregorianCalendar dateCal = new GregorianCalendar(year,month-1,day);
		return dateCal.getTime();
	}

	@SuppressWarnings("unchecked")
	protected String getUserName(Map pageData) {
		String username = ((String)pageData.get("xuser")).split(":")[1];
		return username;
	}
}
